package com.cn.learn.creator.service.impl;

import com.cn.learn.creator.annotation.Pagination;
import com.cn.learn.creator.domain.entity.Column;
import com.cn.learn.creator.domain.entity.DataBase;
import com.cn.learn.creator.domain.entity.Table;
import com.cn.learn.creator.domain.vo.ResultVO;
import com.cn.learn.creator.mapper.ColumnMapper;
import com.cn.learn.creator.mapper.DataBaseMapper;
import com.cn.learn.creator.mapper.TableMapper;
import com.cn.learn.creator.service.IDataBaseService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DataBaseServiceImpl
 * @Description 数据库服务层实现类
 * @Author JiJiang
 * @Date 2023/7/8 13:43
 * @Version 1.0
 */
@Log4j2
@Service
public class DataBaseServiceImpl implements IDataBaseService {


    /**
     * 数据表字段数据访问对象
     */
    @Resource
    private ColumnMapper columnMapper;

    /**
     * 数据表数据访问对象
     */
    @Resource
    private TableMapper tableMapper;

    /**
     * 数据库数据访问对象
     */
    @Resource
    private DataBaseMapper dataBaseMapper;

    @Resource
    private DataSource dataSource;

    @Resource
    private RedisTemplate redisTemplate;


    /**
     * 根据条件查询字段列表
     *
     * @param conditions
     * @return
     */
    @Override
    public ResultVO<List<Column>> searchColumnsByConditions(Column conditions) {

        List<Column> retList = columnMapper.selectColumnListByConditions(conditions);

        return ResultVO.getResultWithCountByList(retList);
    }

    /**
     * 根据条件查询数据表列表
     *
     * @param conditions
     * @return
     */
    @Pagination
    @Override
    public ResultVO<List<Table>> searchTablesByConditions(Table conditions) {

        List<Table> retList = tableMapper.selectTableListByConditions(conditions);

        return ResultVO.getResultWithCountByList(retList);
    }

    /**
     * 根据条件查询数据库列表
     *
     * @param conditions
     * @return
     */
    @Override
    public ResultVO<List<DataBase>> searchDataBasesByConditions(DataBase conditions) {

        List<DataBase> dataBases = dataBaseMapper.selectDataBaseListByConditions(conditions);

        return ResultVO.getResultWithCountByList(dataBases);
    }


    //region 私有方法

    /**
     * 根据数据源获取当前所有数据库的名字
     *
     * @param dataSource
     * @return
     */
    private List<String> getDataBaseNamesByDataSource(DataSource dataSource) {

        if (dataSource == null) {
            return null;
        }


        List<String> retList = new ArrayList<>();
        try {


            ResultSet resultSet = (dataSource.getConnection()).getMetaData().getCatalogs();

            // 遍历数据库列表
            while (resultSet.next()) {
                String databaseName = resultSet.getString("TABLE_CAT");
                log.info("Database Name: " + databaseName);
                retList.add(databaseName);
            }


        } catch (SQLException e) {
            log.error("类：{}执行方法：{}时出现异常！异常信息如下：\n{}", this.getClass().getName(), "getDataBaseNamesByDataSource", e.getMessage());
            retList = null;
        }
        return retList;
    }

    //endregion
}
